﻿using EShop.Net.IRepository.UnitOfWork;
using EShop.Net.IRepository.User;
using EShop.Net.Loging;
using EShop.Net.Model.Entity;
using EShop.Net.Util.Date;
using NLog;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EShop.Net.Repository.User
{
    public class UserRepository : BaseRepository<UserInfo>, IUserRepository
    {
        public UserRepository(IUnitOfWork unitOfWork) : base((UnitOfWork.UnitOfWork)unitOfWork)
        {
        }

        /// <summary>
        /// 商家端登录
        /// </summary>
        /// <param name="userName">账号</param>
        /// <param name="userPwd">密码</param>
        /// <returns>用户信息</returns>
        public async Task<UserInfo> AdminLogin(string userName, string userPwd)
        {
            //try
            //{
            //    return await DbClient.Queryable<UserInfo>()
            //        .Where(u => u.userName.Equals(userName) && u.password.Equals(userPwd))
            //        .FirstAsync();
            //}
            //catch (Exception ex)
            //{
            //    NLogUtil.WriteAll(LogLevel.Error, LogType.DataBase, "商家端登录", "AdminLogin仓储层", ex);
            //    return null;
            //}
            return null;
        }

        /// <summary>
        /// 查询用户根据OpenId
        /// </summary>
        /// <param name="openId">openId</param>
        /// <returns>实体数据</returns>
        public async Task<UserInfo> SelectUserByOpenId(string openId)
        {
            try
            {
                return await DbClient.Queryable<UserInfo>()
                    .Where(u => u.openId.Equals(openId))
                    .FirstAsync();
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(LogLevel.Error, LogType.DataBase, "查询用户根据OpenId", "SelectUserByOpenId仓储层", ex);
                return null;
            }
        }

        /// <summary>
        /// 更新用户sessionKey，根据openid
        /// </summary>
        /// <param name="openid">openid</param>
        /// <param name="sessionKey">sessionKey</param>
        /// <param name="nickName">昵称</param>
        /// <param name="avatarImage">头像路径</param>
        /// <param name="city">省</param>
        /// <param name="province">城市</param>
        /// <returns>成功或失败</returns>
        public async Task<bool> UpdateByUserOpenid(string openid, string sessionKey, string nickName, string avatarImage)
        {
            try
            {
                return await DbClient.Updateable<UserInfo>()
                    .SetColumns(u => new UserInfo()
                    {
                        sessionKey = sessionKey,
                        nickName = nickName,
                        avatarImage = avatarImage,
                        updateTime = DateTimeHelper.BeijingTimeToUnixTimeStamp13(DateTime.Now)
                    })
                    .Where(u => u.openId == openid)
                    .ExecuteCommandHasChangeAsync();
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(LogLevel.Error, LogType.DataBase, "更新用户信息，根据openid", "UpdateByUserOpenid仓储层", ex);
                return false;
            }
        }

        /// <summary>
        /// 根据ID修改个人信息
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="birthday">生日</param>
        /// <param name="gender">性别</param>
        /// <param name="city">城市</param>
        /// <param name="province">省份</param>
        /// <param name="district">区</param>
        /// <param name="userPhoneEncrypt">经过加密的手机号</param>
        /// <returns>成功或失败</returns>
        public async Task<bool> UpdateUserInfoById(int id, DateTime birthday, int gender, string city, string province, string district, string userPhoneEncrypt)
        {
            try
            {
                return await DbClient.Updateable<UserInfo>()
                    .SetColumns(u => new UserInfo
                    {
                        birthday = birthday,
                        gender = gender,
                        city = city,
                        province = province,
                        district = district,
                        userPhone = userPhoneEncrypt,
                        updateTime = DateTimeHelper.BeijingTimeToUnixTimeStamp13(DateTime.Now)
                    })
                    .Where(u => u.id == id)
                    .ExecuteCommandHasChangeAsync();
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(LogLevel.Error, LogType.DataBase, "根据ID修改个人信息", "UpdateUserInfoById仓储层", ex);
                return false;
            }
        }
    }
}
